package com.sinothk.gpserver.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.gpserver.data.DataConstant;
import com.sinothk.gpserver.modules.biz.entity.GroupEntity;
import com.sinothk.gpserver.modules.biz.entity.GroupMemberEntity;
import com.sinothk.gpserver.modules.biz.entity.InviteEntity;
import com.sinothk.gpserver.modules.biz.mapper.GroupMapper;
import com.sinothk.gpserver.modules.biz.mapper.GroupMemberMapper;
import com.sinothk.gpserver.modules.biz.mapper.InviteMapper;
import com.sinothk.gpserver.modules.biz.service.IInviteService;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.gpserver.modules.system.entity.UserEntity;
import com.sinothk.gpserver.modules.system.mapper.UserMapper;
import com.sinothk.gpserver.util.PageDataUtil;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 好友邀请功能 服务实现类
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-09-11
 */
@Service
public class InviteServiceImpl extends ServiceImpl<InviteMapper, InviteEntity> implements IInviteService {

    @Resource
    private InviteMapper inviteMapper;
    @Resource
    private GroupMemberMapper groupMemberMapper;
    @Resource
    private GroupMapper groupMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 查询分页数据
     *
     * @param pageVo 分页参数
     * @return PageData
     */
    @Override
    public PageData<InviteEntity> page(PageVo<InviteEntity> pageVo) {
        Page<InviteEntity> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());

        LambdaQueryWrapper<InviteEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InviteEntity::getToAccount, pageVo.getData().getToAccount());

        IPage<InviteEntity> iPage = inviteMapper.selectPage(page, wrapper);
        return new PageDataUtil<InviteEntity>().create(iPage);
    }

    @Override
    @Transactional(rollbackFor = BizException.class)
    public String join(String inviteId, int state) {
        InviteEntity inviteEntity = this.getById(inviteId);
        LoginEntity currUser = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        if (state == 1) {
            GroupEntity group = groupMapper.selectById(inviteEntity.getFromBizId());
            // 查看是否存在
            LambdaQueryWrapper<GroupMemberEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(GroupMemberEntity::getGroupId, group.getId())
                    .eq(GroupMemberEntity::getMemberAccount, currUser.getAccount());
            List<GroupMemberEntity> groupMemberList = groupMemberMapper.selectList(wrapper);

            if (groupMemberList == null || groupMemberList.size() == 0) {
                // 群成员
                GroupMemberEntity groupMember = new GroupMemberEntity();
                groupMember.setGroupId(group.getId());
                groupMember.setGroupName(group.getGroupName());
                groupMember.setMemberAccount(currUser.getAccount());
                groupMember.setMemberCoins(0f);
                groupMember.setGroupRole(DataConstant.RoleGroup.G_MEMBER);
                groupMember.setCreateBy(currUser.getUserName());
                // 邀请人账号
                groupMember.setInviteBy(inviteEntity.getFromAccount());
                groupMemberMapper.insert(groupMember);

                int groupMemberSize = group.getGroupMemberSize() == null ? 0 : group.getGroupMemberSize();
                group.setGroupMemberSize(groupMemberSize + 1);
                groupMapper.updateById(group);
            }
        }
        // 删除记录
        this.removeById(inviteId);
        return "操作成功";
    }

    @Override
    @Transactional(rollbackFor = BizException.class)
    public String exit(String bizId, String bizType) {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        LambdaQueryWrapper<GroupMemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GroupMemberEntity::getGroupId, bizId)
                .eq(GroupMemberEntity::getMemberAccount, login.getAccount());
        GroupMemberEntity groupMember = groupMemberMapper.selectOne(wrapper);

        if (groupMember != null && groupMember.getGroupRole() == 5) throw new BizException("管理员不能退出俱乐部");

        // 其他人直接退出
        groupMemberMapper.delete(wrapper);

        // 更新群数量
        GroupEntity group = groupMapper.selectById(bizId);
        int groupMemberSize = group.getGroupMemberSize() == null ? 0 : group.getGroupMemberSize();
        if (groupMemberSize > 1) {
            group.setGroupMemberSize(groupMemberSize - 1);
            groupMapper.updateById(group);
        }
        return "操作成功";
    }

    @Override
    public String add(InviteEntity inviteEntity) {

        LambdaQueryWrapper<GroupMemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GroupMemberEntity::getGroupId, inviteEntity.getFromBizId())
                .eq(GroupMemberEntity::getMemberAccount, inviteEntity.getToAccount());
        List<GroupMemberEntity> list = groupMemberMapper.selectList(wrapper);
        if (list != null && list.size() > 0) throw new BizException("对方已是俱乐部成员");

        inviteEntity.setInviteMsg("邀你加入俱乐部...");

        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();

        UserEntity userEntity = userMapper.selectByAccount(login.getAccount());
        inviteEntity.setFromAccount(userEntity.getAccount());
        inviteEntity.setFromName(userEntity.getUserName());
        inviteEntity.setFromAvatar(userEntity.getUserAvatar());
        inviteEntity.setCreateBy(userEntity.getUserName());
        // 邀请类型：0好友邀请；1入群邀请
        inviteEntity.setFromType(1);

        inviteMapper.insert(inviteEntity);
        return "操作成功";
    }

    @Override
    public Integer inviteTipsForUser() {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();

        LambdaQueryWrapper<InviteEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InviteEntity::getToAccount, login.getAccount());
        return inviteMapper.selectCount(queryWrapper);
    }
}
